<?php


class Menu_model extends Base_model {

    /**
     * 表名
     * 
     * @param string 
     */
    public $_table_name = "admin_menu";


    /**
     * status状态：1 正常    0 禁用(禁用时, 属于此角色的所有用户将无法登录)
     */
    const STATUS_SUCCESS = 1;
    const STATUS_FAILED = 0;


     /**
     * 状态数组
     * @var array 
     */
    public static $status_arr = array(self::STATUS_SUCCESS => '正常', self::STATUS_FAILED => '禁用');


    /**
     * 状态
     * @var string 
     */
    public $status;


    /**
     * 角色名称
     * @var string 
     */
    public $name;




    /**
     * 插入数据前验证
     * 
     * @return boolean 
     */
    protected function prepare_insert() {

        if(empty($this->name) OR empty($this->url))
        {
            return false;
        }

        if(empty($this->status)) {
            $this->status = self::STATUS_SUCCESS;
        }

        return true;
    }

    
    /**
     * 搜索
     * 
     * @return boolean true | false
     */
    public function search() {
        $sql = "SELECT ";

        if (!empty($this->_is_search_counter))
        {
            $sql .= " COUNT(adro.id) AS rows_total ";
        }
        else
        {
            $sql .= " adro.*, COUNT(id) AS admin_counts ";
        }

        $sql .= " FROM {$this->_table_name} AS adro ";

        // if(empty($this->_is_search_counter))
        // {
        //     $sql .= " LEFT JOIN {$this->db->dbprefix}admin AS admn ON admn.role_id = adro.id ";
        // }

        $statement = "";
        $where = array();

        if (!empty($this->id)) {
            $statement .= " AND adro.id = ? ";
            $where[] = $this->id;
        }

        if (!empty($this->status)) {
            $statement .= " AND adro.status = ? ";
            $where[] = $this->status;
        }

        //LIKE搜索转义
        if (!empty($this->name)) {
            $statement .= " AND adro.name LIKE '%" . $this->db->escape_like_str($this->name). "%' ";
        }

        //处理条件
        if($statement) {
            $sql .= " WHERE " . substr($statement, 4);
        }


        if (!empty($this->_is_search_counter))
        {
            $sql .= " LIMIT 1";
        }
        else
        {
            $sql .= " GROUP BY adro.id ";

            if (!empty($this->_order_by)) {
                $sql .= " ORDER BY {$this->_order_by} ";
            }

            //是否分页
            if ($this->is_paging()) {

                $page_from = ($this->_page_index - 1) * $this->_page_count;

                $sql .= " LIMIT {$page_from}, {$this->_page_count}";
            }
        }

        $result = $this->db->query($sql, $where);

        if(empty($this->_is_search_counter))
        {
            $this->_search_results = $result->result();
        }
        else
        {
            if ($result->num_rows() > 0) {
                $row = $result->row();

                if (!empty($row->rows_total)) {
                    $this->_search_num_rows = $row->rows_total;
                }
            }
        }

        $result->free_result();

        return true;
    }


    //获取所有的模块列表
    public function get_all_menu()
    {
        $sql = "SELECT  adro.id, adro.status, adro.url, adro.name, adro.parent_id, adro.context, adro.icon, adro.tabid, '' as parent FROM {$this->_table_name} AS adro where  adro.status = 1";
        $result = $this->db->query($sql);
        return $result->result();
    }

    //获取下一级模块列表
    public function get_down_menu($parent_id)
    {
        $sql = "SELECT  adro.id, adro.status, adro.url, adro.name, adro.parent_id, adro.tabid,adro.icon, adro.context FROM {$this->_table_name} AS adro where parent_id = ".$parent_id;
        $result = $this->db->query($sql);
        return $result->result();
    }    

    public function get_up_menu($id)
    {
        $sql = "SELECT  adro.id, adro.status, adro.url, adro.name, adro.parent_id, adro.tabid,adro.icon, adro.context FROM {$this->_table_name} AS adro where id in (select parent_id from {$this->_table_name} where   id = ".$id.")";
        $result = $this->db->query($sql);
        return $result->result();
    }   


    public function get_menu_select(){
        $a = $this->get_all_menu();
        $a = object_to_array($a);
        $s = $this->getDesc($a,0);
        return $s;
    }

    public function getDesc($a,$parent_id = 0)
    {
        $s = [];
        foreach ($a as $key => $value) {
            $k = [];
            if($value['parent_id'] == $parent_id){
                $k =  $value;
                $k['child'] = $this->getDesc($a, $value["id"]);
                $s[] = $k;
            }
        }
        return $s;
    }

  


}